package cn.niit.dao;

import cn.niit.entity.SysResource;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysResourceMapper {
    @Insert({
        "insert into tb_sys_resource (id, parent_id, ",
        "resource_name, resource_sort, ",
        "resource_href, resource_icon, ",
        "permission, remarks, ",
        "resource_level, del_flag, ",
        "status, type, gmt_create, ",
        "create_user_id, create_user_name, ",
        "gmt_update, modify_user_id, ",
        "modify_user_name)",
        "values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, ",
        "#{resourceName,jdbcType=VARCHAR}, #{resourceSort,jdbcType=INTEGER}, ",
        "#{resourceHref,jdbcType=VARCHAR}, #{resourceIcon,jdbcType=VARCHAR}, ",
        "#{permission,jdbcType=VARCHAR}, #{remarks,jdbcType=VARCHAR}, ",
        "#{resourceLevel,jdbcType=TINYINT}, #{delFlag,jdbcType=BIT}, ",
        "#{status,jdbcType=BIT}, #{type,jdbcType=VARCHAR}, #{gmtCreate,jdbcType=TIMESTAMP}, ",
        "#{createUserId,jdbcType=BIGINT}, #{createUserName,jdbcType=VARCHAR}, ",
        "#{gmtUpdate,jdbcType=TIMESTAMP}, #{modifyUserId,jdbcType=BIGINT}, ",
        "#{modifyUserName,jdbcType=VARCHAR})"
    })
    int insert(SysResource record);

    int insertSelective(SysResource record);

    /**
     * 根据用户ID查询所有权限标识
     * @param userId
     * @return
     */
    @Select("SELECT t3.permission " +
            "FROM `tb_sys_user_role` t1,`tb_sys_role_resource` t2,`tb_sys_resource` t3 " +
            "WHERE t1.`user_id`=#{userId} AND t1.`role_id`=t2.`role_id` " +
            "AND t2.`resource_id`=t3.`id` AND t3.STATUS=1 AND t3.del_flag=0 " +
            "AND t3.permission<> ''")
    List<String> listUserPermissions(@Param("userId") Long userId);

    /**
     * 根据用户Id 查询该用户可访问的菜单
     */
    List<SysResource> getMenuListByUserId(Long userId);


    /**
     * 根据用户Id 查询该用户可访问的按钮
     */
    List<SysResource> getButtonListByUserId(Long userId);
}